<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Catalog extends CI_Controller {
     
    public  $page_name ="catalog";
     public function __construct(){
        parent::__construct();
    if(!$this->session->userdata('is_login')){
         redirect("/admin/auth/index");
 }
}


    public function index(){
    $this->load->model('Catalog_model');
    $catalogs = $this->Catalog_model->get_all();
     $data = array(
       'catalogs'=>$catalogs
        );
     $this->load->view('admin/Catalog',$data);

}
 public function delete($id){
        $this->load->model("Catalog_model");
        $this->Catalog_model->delete($id);
        redirect("admin/catalog/index");
    }
 
         public function add()
        {
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
             $this->form_validation->set_rules('name', '名称', 'required');
            $this->load->model('Catalog_model');


            if ($this->form_validation->run() == FALSE)
            {
                
               $catalog = new stdClass();
               $catalog->name = "";
                
               $data = array(
                   'catalog'=>$catalog,
                   'title'=>"添加",
                    );
                $this->load->view('admin/Catalog_form', $data);
            }
            else
            {
            $data=$this->input->post();
         
            $this->Catalog_model->add_one($data);
            redirect("admin/catalog/index");

                
            }
        }
        
      public function edit($id)
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('name', '名称', 'required');
        $this->load->model('Catalog_model');
         $catalogs=$this->Catalog_model->get_all();
        
        if ($this->form_validation->run() == FALSE)
        {
             $data = array(
               'title'=>"编辑",
               'catalog'=>$this->Catalog_model->get_one($id),
              
            );
           
            $this->load->view('admin/Catalog_form', $data);
        }
        else
        {
        $data=$this->input->post();
        $this->load->model("Catalog_model");
        $this->Catalog_model->edit_one($id,$data);
        redirect("admin/Catalog/index");

            
        }
    }
   

}
